package com.weijun.keepalive.utils

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.weijun.keepalive.keepAlive

/**
 * @author: weijun
 * date: 2023/7/6
 * description:
 */
class NotificationUtil(private val context: Context) : ContextWrapper(context) {

    private val channelId = context.packageName
    private val appName = context.packageName

    private lateinit var channel: NotificationChannel
    val manager: NotificationManager
        get() = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    private val pdIntent:Int
        get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_UPDATE_CURRENT

    @RequiresApi(api = Build.VERSION_CODES.O)
    fun createNotificationChannel() {
        if (::channel.isInitialized.not()) {
            channel = NotificationChannel(channelId, appName, NotificationManager.IMPORTANCE_HIGH).apply {
                enableLights(false)//是否在桌面icon右上角展示小红点
                enableVibration(false)
                vibrationPattern = longArrayOf(0) //震动模式
                setSound(null, null) //声音
            }
            manager.createNotificationChannel(channel)
        }
    }

    private val pendingIntent:(Intent) -> PendingIntent = {
        //PendingIntent.FLAG_UPDATE_CURRENT 这个类型才能传值
        PendingIntent.getBroadcast(context, 0, it, pdIntent)
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    fun getChannelNotification(intent: Intent):NotificationCompat.Builder{
        return NotificationCompat.Builder(this, channelId)
            .setContentTitle(keepAlive?.notificationConfig?.title)
            .setContentText(keepAlive?.notificationConfig?.description)
            .setSmallIcon(keepAlive?.notificationConfig?.iconRes ?: 0)
            .setContentIntent(pendingIntent.invoke(intent))
            .setAutoCancel(true) //用户触摸时，自动关闭
//            .setOngoing(true) //设置处于运行状态
    }

    fun getChannelNotification_25(intent: Intent):NotificationCompat.Builder{
        return NotificationCompat.Builder(this, channelId)
            .setContentTitle(keepAlive?.notificationConfig?.title)
            .setContentText(keepAlive?.notificationConfig?.description)
            .setSmallIcon(keepAlive?.notificationConfig?.iconRes ?: 0)
            .setContentIntent(pendingIntent.invoke(intent))
            .setAutoCancel(true) //用户触摸时，自动关闭
            .setVibrate(longArrayOf(0))
//            .setOngoing(true) //设置处于运行状态
    }
}

fun Context.sendNotification(intent: Intent) {
    NotificationUtil(this).apply {
        manager.notify((0..10000).random(), if (Build.VERSION.SDK_INT >= 26) {
            createNotificationChannel()
            getChannelNotification(intent).build()
        } else {
            getChannelNotification_25(intent).build()
        })
    }
}

fun Context.createNotification(intent: Intent): Notification =
    NotificationUtil(this).run {
        if (Build.VERSION.SDK_INT >= 26) {
            createNotificationChannel()
            getChannelNotification(intent).build()
        } else {
            getChannelNotification_25(intent).build()
        }
    }